
<!DOCTYPE html>

<html>
  
<!-- Mirrored from docs.sympy.org/latest/special_topics/classification.html by HTTrack Website Copier/3.x [XR&CO'2014], Sat, 15 Jan 2022 03:25:56 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=utf-8" /><!-- /Added by HTTrack -->
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />

    <title>Classification of SymPy objects &#8212; SymPy 1.9 documentation</title>
    <link rel="stylesheet" type="text/css" href="../_static/pygments.css" />
    <link rel="stylesheet" type="text/css" href="../_static/default.css" />
    <link rel="stylesheet" type="text/css" href="../_static/graphviz.css" />
    <link rel="stylesheet" type="text/css" href="../_static/plot_directive.css" />
    <link rel="stylesheet" type="text/css" href="../../../live.sympy.org/static/live-core.css" />
    <link rel="stylesheet" type="text/css" href="../../../live.sympy.org/static/live-autocomplete.css" />
    <link rel="stylesheet" type="text/css" href="../../../live.sympy.org/static/live-sphinx.css" />
    
    <script data-url_root="../" id="documentation_options" src="../_static/documentation_options.js"></script>
    <script src="../_static/jquery.js"></script>
    <script src="../_static/underscore.js"></script>
    <script src="../_static/doctools.js"></script>
    <script src="../../../live.sympy.org/static/utilities.js"></script>
    <script src="../../../live.sympy.org/static/external/classy.js"></script>
    <script src="../../../live.sympy.org/static/live-core.js"></script>
    <script src="../../../live.sympy.org/static/live-autocomplete.js"></script>
    <script src="../../../live.sympy.org/static/live-sphinx.js"></script>
    <script async="async" src="../../../cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest8331.js?config=TeX-AMS_HTML-full"></script>
    <script type="text/x-mathjax-config">MathJax.Hub.Config({"tex2jax": {"inlineMath": [["\\(", "\\)"]], "displayMath": [["\\[", "\\]"]]}})</script>
    
    <link rel="shortcut icon" href="../_static/sympy-notailtext-favicon.ico"/>
    <link href="classification.html" rel="canonical" />
    
    <link rel="index" title="Index" href="../genindex.html" />
    <link rel="search" title="Search" href="../search.html" />
    <link rel="next" title="Wiki" href="../wiki.html" />
    <link rel="prev" title="Finite Difference Approximations to Derivatives" href="finite_diff_derivatives.html" /> 
  </head><body>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="../wiki.html" title="Wiki"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="finite_diff_derivatives.html" title="Finite Difference Approximations to Derivatives"
             accesskey="P">previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="../index.html">SymPy 1.9 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="index.html" accesskey="U">SymPy Special Topics</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="#">Classification of SymPy objects</a></li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <section id="classification-of-sympy-objects">
<h1>Classification of SymPy objects<a class="headerlink" href="#classification-of-sympy-objects" title="Permalink to this headline">¶</a></h1>
<p>There are several ways of how SymPy object is classified.</p>
<section id="class">
<h2>class<a class="headerlink" href="#class" title="Permalink to this headline">¶</a></h2>
<p>Like any other object in Python, SymPy expression is an instance of class. You can
get the class of the object with built-in <span class="math notranslate nohighlight">\(type()\)</span> function, and check it with
<span class="math notranslate nohighlight">\(isinstance()\)</span> function.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">Add</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.abc</span> <span class="kn">import</span> <span class="n">x</span><span class="p">,</span><span class="n">y</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">type</span><span class="p">(</span><span class="n">x</span> <span class="o">+</span> <span class="n">y</span><span class="p">)</span>
<span class="go">&lt;class &#39;sympy.core.add.Add&#39;&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">isinstance</span><span class="p">(</span><span class="n">x</span> <span class="o">+</span> <span class="n">y</span><span class="p">,</span> <span class="n">Add</span><span class="p">)</span>
<span class="go">True</span>
</pre></div>
</div>
<p>Classes represent only the programmatic structures of the objects, and does not
distinguish the mathematical difference between them. For example, the integral
of number and the integral of matrix both have the class <span class="math notranslate nohighlight">\(Integral\)</span>, although the
former is number and the latter is matrix.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">MatrixSymbol</span><span class="p">,</span> <span class="n">Integral</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">A</span> <span class="o">=</span> <span class="n">MatrixSymbol</span><span class="p">(</span><span class="s1">&#39;A&#39;</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">type</span><span class="p">(</span><span class="n">Integral</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">x</span><span class="p">))</span>
<span class="go">&lt;class &#39;sympy.integrals.integrals.Integral&#39;&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">type</span><span class="p">(</span><span class="n">Integral</span><span class="p">(</span><span class="n">A</span><span class="p">,</span> <span class="n">x</span><span class="p">))</span>
<span class="go">&lt;class &#39;sympy.integrals.integrals.Integral&#39;&gt;</span>
</pre></div>
</div>
</section>
<section id="kind">
<h2>kind<a class="headerlink" href="#kind" title="Permalink to this headline">¶</a></h2>
<p>Kind indicates what mathematical object does the expression represent.
You can retrieve the kind of expression with <span class="math notranslate nohighlight">\(.kind\)</span> property.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">Integral</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">x</span><span class="p">)</span><span class="o">.</span><span class="n">kind</span>
<span class="go">NumberKind</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Integral</span><span class="p">(</span><span class="n">A</span><span class="p">,</span> <span class="n">x</span><span class="p">)</span><span class="o">.</span><span class="n">kind</span>
<span class="go">MatrixKind(NumberKind)</span>
</pre></div>
</div>
<p>This result shows that <span class="math notranslate nohighlight">\(Integral(1, x)\)</span> is number, and <span class="math notranslate nohighlight">\(Integral(A, x)\)</span> is matrix with number element.</p>
<p>Since the class cannot guarantee to catch this difference, kind of the object is very important.
For example, if you are building a function or class that is designed to work only for
numbers, you should consider filtering the arguments with <span class="math notranslate nohighlight">\(NumberKind\)</span> so that the user
does not naively pass unsupported objects such as <span class="math notranslate nohighlight">\(Integral(A, x)\)</span>.</p>
<p>For the performance, set theory is not implemented in kind system. For example,</p>
<blockquote>
<div><p><span class="math notranslate nohighlight">\(NumberKind\)</span> does not distinguish the real number and complex number.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">pi</span><span class="p">,</span> <span class="n">I</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">pi</span><span class="o">.</span><span class="n">kind</span>
<span class="go">NumberKind</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">I</span><span class="o">.</span><span class="n">kind</span>
<span class="go">NumberKind</span>
</pre></div>
</div>
<p>SymPy’s <span class="math notranslate nohighlight">\(Set\)</span> and kind are not compatible.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">S</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.core.kind</span> <span class="kn">import</span> <span class="n">NumberKind</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">S</span><span class="o">.</span><span class="n">Reals</span><span class="o">.</span><span class="n">is_subset</span><span class="p">(</span><span class="n">S</span><span class="o">.</span><span class="n">Complexes</span><span class="p">)</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">S</span><span class="o">.</span><span class="n">Reals</span><span class="o">.</span><span class="n">is_subset</span><span class="p">(</span><span class="n">NumberKind</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
<span class="c">...</span>
<span class="gr">ValueError</span>: <span class="n">Unknown argument &#39;NumberKind&#39;</span>
</pre></div>
</div>
</div></blockquote>
</section>
<section id="sets-and-assumptions">
<h2>sets and assumptions<a class="headerlink" href="#sets-and-assumptions" title="Permalink to this headline">¶</a></h2>
<p>If you want to classify the object in strictly mathematical way, you may need
SymPy’s sets and assumptions.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">ask</span><span class="p">,</span> <span class="n">Q</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">S</span><span class="o">.</span><span class="n">One</span> <span class="ow">in</span> <span class="n">S</span><span class="o">.</span><span class="n">Reals</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ask</span><span class="p">(</span><span class="n">Q</span><span class="o">.</span><span class="n">even</span><span class="p">(</span><span class="mi">2</span><span class="o">*</span><span class="n">x</span><span class="p">),</span> <span class="n">Q</span><span class="o">.</span><span class="n">odd</span><span class="p">(</span><span class="n">x</span><span class="p">))</span>
<span class="go">True</span>
</pre></div>
</div>
<p>See <span class="math notranslate nohighlight">\(assumptions\)</span> module and <span class="math notranslate nohighlight">\(sets\)</span> module for more information.</p>
</section>
<section id="func">
<h2>func<a class="headerlink" href="#func" title="Permalink to this headline">¶</a></h2>
<p><span class="math notranslate nohighlight">\(func\)</span> is the head of the object, and it is used to recurse over the expression tree.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">Add</span><span class="p">(</span><span class="n">x</span> <span class="o">+</span> <span class="n">y</span><span class="p">)</span><span class="o">.</span><span class="n">func</span>
<span class="go">&lt;class &#39;sympy.core.add.Add&#39;&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Add</span><span class="p">(</span><span class="n">x</span> <span class="o">+</span> <span class="n">x</span><span class="p">)</span><span class="o">.</span><span class="n">func</span>
<span class="go">&lt;class &#39;sympy.core.mul.Mul&#39;&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Q</span><span class="o">.</span><span class="n">even</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">.</span><span class="n">func</span>
<span class="go">&lt;class &#39;sympy.assumptions.assume.AppliedPredicate&#39;&gt;</span>
</pre></div>
</div>
<p>As you can see, resulting head may be a class or another SymPy object.
Keep this in mind when you classify the object with this attribute.
See <a class="reference internal" href="../tutorial/manipulation.html#tutorial-manipulation"><span class="std std-ref">Advanced Expression Manipulation</span></a> for detailed information.</p>
</section>
</section>


            <div class="clearer"></div>
          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
            <p class="logo"><a href="../index.html">
              <img class="logo" src="../_static/sympylogo.png" alt="Logo"/>
            </a></p>
  <h3><a href="../index.html">Table of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Classification of SymPy objects</a><ul>
<li><a class="reference internal" href="#class">class</a></li>
<li><a class="reference internal" href="#kind">kind</a></li>
<li><a class="reference internal" href="#sets-and-assumptions">sets and assumptions</a></li>
<li><a class="reference internal" href="#func">func</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="finite_diff_derivatives.html"
                        title="previous chapter">Finite Difference Approximations to Derivatives</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="../wiki.html"
                        title="next chapter">Wiki</a></p>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="../_sources/special_topics/classification.rst.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3 id="searchlabel">Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="https://docs.sympy.org/latest/search.html" method="get">
      <input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
      <input type="submit" value="Go" />
    </form>
    </div>
</div>
<script>$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="../wiki.html" title="Wiki"
             >next</a> |</li>
        <li class="right" >
          <a href="finite_diff_derivatives.html" title="Finite Difference Approximations to Derivatives"
             >previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="../index.html">SymPy 1.9 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="index.html" >SymPy Special Topics</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="#">Classification of SymPy objects</a></li> 
      </ul>
    </div>
    <div class="footer" role="contentinfo">
        &#169; Copyright 2021 SymPy Development Team.
      Last updated on Sep 30, 2021.
      Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.1.2.
    </div>
  </body>

<!-- Mirrored from docs.sympy.org/latest/special_topics/classification.html by HTTrack Website Copier/3.x [XR&CO'2014], Sat, 15 Jan 2022 03:25:57 GMT -->
</html>